<?php

namespace App\Service;

use App\Models\Base\LogMerge;
use App\Models\Base\ServerList;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

/**
 * 游戏区服逻辑.
 */
class ServerOnlineService
{
    /**
     * [getServerDb 获取游戏区服所有数据库配置].
     *
     * @Author   王文凡
     * @DateTime 2018-02-27
     *
     * @version  1.0
     *
     * @param int $server_id [区服id]
     *
     * @return object
     */
    public function getServerDb($server_id = 0)
    {
        if ($server_id) {
            $server_data = DB::table('server_onlines')->where('server_id', $server_id)->first();
        } else {
            $server_data = DB::table('server_onlines')->first();
        }

        return $server_data;
    }

    /**
     * [getServerGameDb 获取区服游戏数据库配置].
     *
     * @Author   王文凡
     * @DateTime 2018-02-27
     *
     * @version  1.0
     *
     * @param int $server_id [区服id]
     *
     * @return null|array
     */
    public function getServerGameDb($server_id = 0)
    {
        if ($data = $this->getServerDb($server_id)) {
            if (isset($data->config_game_server)) {
                $config = json_decode($data->config_game_server, true);

                return TransformDbConfig($config);
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    /**
     * [getServerOnline 获取所有游戏区服].
     *
     * @Author   王文凡
     * @DateTime 2018-02-28
     *
     * @version  1.0
     *
     * @return [type] [description]
     */
    public function getServerOnline()
    {
        return ServerList::all();
    }

    /**
     * [getServerGameDbAll 获取服务器所有游戏数据库配置].
     *
     * @Author   王文凡
     * @DateTime 2018-03-06
     *
     * @version  1.0
     *
     * @param string $attribute 数据库属性
     *
     * @return [type] [description]
     */
    public function getServerGameDbAll($attribute = 'game_db')
    {
        $configArray = [];
        if ($data = $this->getServerOnline()) {
            foreach ($data as $key => $value) {
                if ($config_game_server = $value->$attribute) {
                    $config = json_decode($config_game_server, true);
                    $configArray[$value->server_id] = TransformDbConfig($config);
                }
            }
        }

        return empty($configArray) ? null : $configArray;
    }

    /**
     * [serverExist 查询服务器是否存在].
     *
     * @Author   王文凡
     * @DateTime 2018-03-09
     *
     * @version  1.0
     *
     * @param int|array $ids [服务器id]
     *
     * @return
     */
    public static function serverExist($id = '')
    {
        if (!is_array($id)) {
            $ids[] = $id;
        } else {
            $ids = $id;
        }

        return DB::table('server_lists')->whereIn('server_id', $ids)->get()->count();
    }

    /**
     * [mergeLog 更新已合服的物理服id].
     *
     * @Author   王文凡
     * @DateTime 2018-03-09
     *
     * @version  1.0
     *
     * @param int    $from_id [被合服id]
     * @param int    $to_id   [合服id]
     * @param string $section [区间id]
     *
     * @return
     */
    public static function mergeLog($from_id, $to_id, $section)
    {
        $merge_ids = explode('-', $section);
        LogMerge::whereIn('from_id', $merge_ids)->update(['to_id' => $to_id]);
    }

    /**
     * [phyServerId 判断是否被合服并获取物理服的serverid].
     *
     * @Author   王文凡
     * @DateTime 2018-04-25
     *
     * @version  1.0
     *
     * @param [type] $server_id [游戏服id]
     *
     * @return [type] [description]
     */
    public static function mergeServerId($server_id)
    {
        $merge = LogMerge::where('from_id', $server_id)->first();
        if (!$merge) {
            return $server_id;
        }

        return $merge->to_id;
    }

    /**
     * [deleteServer 删除服务器].
     *
     * @Author   王文凡
     * @DateTime 2018-05-10
     *
     * @version  1.0
     *
     * @param array $serverIds [description]
     *
     * @return [type] [description]
     */
    public static function deleteServer($serverIds)
    {
        $now = Carbon::now()->toDateTimeString();

        return ServerList::whereIn('server_id', $serverIds)->update(['type' => 0, 'deleted_at' => $now]);
    }
}
